﻿/*
* 
* TEST = OK
*/
Library.Validation.IsValidDate = function(dateStr) {
    try {
        if (Library.Util.Count(dateStr, ".") == 0)
            return false;
        var t = dateStr.split(".");
        if (parseInt(t[1]) == 1 || parseInt(t[1]) == 3 || parseInt(t[1]) == 5 || parseInt(t[1]) == 7 || parseInt(t[1]) == 8 || parseInt(t[1]) == 10 || parseInt(t[1]) == 12) {
            if (parseInt(t[0]) > 31)
                return false;
        } else if (parseInt(t[1]) == 2) {
            if (((parseInt(t[2]) % 4 == 0) && (parseInt(t[2]) % 100 != 0)) || (parseInt(t[2]) % 400 == 0)) {
                if (parseInt(t[0]) > 29)
                    return false;
            } else {
                if (parseInt(t[0]) > 28)
                    return false;
            }
        } else {
            if (parseInt(t[0]) > 30)
                return false;
        }
        return true;
    } catch (err) {
        return false;
    }
};

/*
* 
* TEST = OK
*/
Library.Validation.IsDate = function(Day, Mn, Yr) {
    var DateVal = Mn + "/" + Day + "/" + Yr;
    var dt = new Date(DateVal);
    if (dt.getDate() != Day) {
        return false;
    } else if (dt.getMonth() != Mn - 1) {
        return false;
    } else if (dt.getFullYear() != Yr) {
        return false;
    }
    return true;
};

/*
* 
* TEST = OK
*/
Library.Validation.IsNumeric = function(deger, ondalikKarakteri) {
    var GecerliKarakterler;
    var IsNumber = true;
    var Karakter;

    GecerliKarakterler = "0123456789" + ondalikKarakteri;

    for (var i = 0; i < deger.length && IsNumber; i++) {
        Karakter = deger.charAt(i);
        if (GecerliKarakterler.indexOf(Karakter) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
};

/*
* 
* TEST = OK
*/
Library.Validation.IsTime = function(deger, format) {
    if (format == "SimpleTime") {
        if (deger.length == 5) {
            var colon1 = deger.charAt(2);
            if (colon1 != ":")
                return false;

            var hours = deger.charAt(0) + deger.charAt(1);
            var minutes = deger.charAt(3) + deger.charAt(4);

            if (hours >= 0 && hours <= 23 && minutes <= 59 && minutes >= 0) {
                return true;
            } else {
                return false;
            }
        } else return false;
    } else {
        if (deger.length == 8) {
            var colon1 = deger.charAt(2);
            var colon2 = deger.charAt(5);
            if (colon1 != ":" || colon2 != ":")
                return false;

            var hours = deger.charAt(0) + deger.charAt(1);
            var minutes = deger.charAt(3) + deger.charAt(4);
            var seconds = deger.charAt(6) + deger.charAt(7);

            if (hours >= 0 && hours <= 23 && minutes <= 59 && minutes >= 0 && seconds <= 59 && seconds >= 0)
                return true;
            else
                return false;
        } else return false;
    }
};

/*
* 
* TEST = OK
*/
Library.Validation.IsTcKimlikNo = function(tcKimlikNo) {
    if (tcKimlikNo == null || tcKimlikNo.length != 11)
        return false;
    if (!Library.Validation.IsNumeric(tcKimlikNo, ""))
        return false;
    if (tcKimlikNo.indexOf("0") == 0)
        return false;

    var t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11;
    var t1 = tcKimlikNo.substr(0, 1);
    var t2 = tcKimlikNo.substr(1, 1);
    var t3 = tcKimlikNo.substr(2, 1);
    var t4 = tcKimlikNo.substr(3, 1);
    var t5 = tcKimlikNo.substr(4, 1);
    var t6 = tcKimlikNo.substr(5, 1);
    var t7 = tcKimlikNo.substr(6, 1);
    var t8 = tcKimlikNo.substr(7, 1);
    var t9 = tcKimlikNo.substr(8, 1);
    var t10 = tcKimlikNo.substr(9, 1);
    var t11 = tcKimlikNo.substr(10, 1);

    var alg1 = (((eval(t1) + eval(t3) + eval(t5) + eval(t7) + eval(t9)) * 7) - (eval(t2) + eval(t4) + eval(t6) + eval(t8))) % 10;
    if (alg1 != t10)
        return false;

    var alg2 = (eval(t1) + eval(t2) + eval(t3) + eval(t4) + eval(t5) + eval(t6) + eval(t7) + eval(t8) + eval(t9) + eval(t10)) % 10;
    if (alg2 != t11)
        return false;

    return true;
};

/*
* 
* TEST = OK
*/
Library.Validation.IsIBAN = function(iban) {
    if (iban == null)
        return false;

    iban = Library.Util.ReplaceAll(iban, "-", "");
    iban = Library.Util.ReplaceAll(iban, " ", "");

    if (iban.length != 26)
        return false;
    if (iban.indexOf("İ") > -1 || iban.indexOf("Ç") > -1 || iban.indexOf("Ğ") > -1 || iban.indexOf("Ö") > -1 || iban.indexOf("Ş") > -1 || iban.indexOf("Ü") > -1)
        return false;

    var iban1 = iban.substr(0, 1);
    var iban2 = iban.substr(1, 1);
    var iban3 = iban.substr(4, 24);

    if (!Library.Validation.IsNumeric(iban3, ""))
        return false;
    if (Library.Validation.IsNumeric(iban1, "") || Library.Validation.IsNumeric(iban2, ""))
        return false;

    var A = 10; var B = 11; var C = 12; var D = 13; var E = 14; var F = 15; var G = 16; var H = 17; var I = 18; var J = 19; var K = 20; var L = 21; var M = 22;
    var N = 23; var O = 24; var P = 25; var Q = 26; var R = 27; var S = 28; var T = 29; var U = 30; var V = 31; var W = 32; var X = 33; var Y = 34; var Z = 35;

    var iban = iban3 + ("" + eval(iban1)) + ("" + eval(iban2)) + iban.substr(2, 2);

    var ibanPart1 = iban.substr(0, 18);
    var ibanPart2 = iban.substr(18);
    var ibanParity = ((eval(ibanPart1 % 97) + "") + ibanPart2 + "") % 97;

    if (ibanParity == 1)
        return true;

    return false;
};

/*
* 
* TEST = OK
*/
Library.Validation.ValidateForm = function () {
    var form = document.forms[0];
    var message = "";
    var newLine = "\n";

    var invalidElements = new Array();

    for (var i = 0; i < form.elements.length; i++) {
        var element = form.elements[i];

        if (element.id == "")
            continue;

        if (element.type == "button" || element.type == "submit")
            continue;

        if (element.id == "_dtoListPageSize")
            continue;

        $("#" + element.id).removeClass("Invalid");

        var validate = Library.UserInterface.GetAttribute(element.id, "Validate");
        var validationName = Library.UserInterface.GetAttribute(element.id, "ValidationName");

        // Hidden validation
        if (element.type == "hidden" && validate != null && validate == "True" && element.value.lenght == 0) {
            message += "* " + validationName + " bilgisi bulunamadı." + newLine;
            Library.Array.Add(invalidElements, element.id);
        }

        // Combo validation
        if (element.type == "select-one" && validate != null && validate == "True" && element.selectedIndex == 0) {
            message += "* " + validationName + " bilgisi seçilmelidir." + newLine;
            Library.Array.Add(invalidElements, element.id);
        }

        // Password validation
        if (element.type == "password" && validate != null && validate == "True" && element.value.length == 0) {
            message += "* " + validationName + " bilgisi girilmelidir." + newLine;
            Library.Array.Add(invalidElements, element.id);
        }

        // File validation
        if (element.type == "file" && validate != null && validate == "True" && element.value.length == 0) {
            message += "* " + validationName + " dosyası seçilmelidir." + newLine;
            Library.Array.Add(invalidElements, element.id);
        }

        if (element.type == "text" || element.type == "textarea") {
            var controlType = Library.UserInterface.GetAttribute(element.id, "ControlType");
            var minLength = Library.UserInterface.GetAttribute(element.id, "ValidationMinLength");
            var maxLength = Library.UserInterface.GetAttribute(element.id, "ValidationMaxLength");

            if (element.value.length != 0 && minLength != null && minLength != "" && minLength != "-1" && eval(minLength) > Library.Util.ReplaceAll(element.value, ".", "").length) {
                message += "* " + validationName + " alanına en az " + minLength + " karakter girilmelidir. Girilen karakter sayısı: " + element.value.length + newLine;
                Library.Array.Add(invalidElements, element.id);
            }
            if (element.value.length != 0 && maxLength != null && maxLength != "" && maxLength != "-1" && eval(maxLength) < Library.Util.ReplaceAll(element.value, ".", "").length) {
                message += "* " + validationName + " alanına en fazla " + maxLength + " karakter girilmelidir. Girilen karakter sayısı: " + element.value.length + newLine;
                Library.Array.Add(invalidElements, element.id);
            }

            // Time Validation
            if (element.value.length != 0 && controlType == "Time" && !Library.Validation.IsTime(element.value, "SimpleTime")) {
                message += "* " + validationName + " bilgisi doğru girilmelidir." + newLine;
                Library.Array.Add(invalidElements, element.id);
            }

            // DateBox validation
            if (element.value.length != 0 && controlType == "Date") {
                if (!Library.Validation.IsNumeric(element.value, ".")) {
                    message += "* " + validationName + " bilgisi doğru girilmelidir." + newLine;
                    Library.Array.Add(invalidElements, element.id);
                }
                if (!Library.Validation.IsValidDate(element.value)) {
                    message += "* " + validationName + " bilgisi doğru girilmelidir." + newLine;
                    Library.Array.Add(invalidElements, element.id);
                }
            }
            // TcKimlik Validation
            if (element.value.length != 0 && controlType == "TcKimlik") {
                if (!Library.Validation.IsNumeric(element.value, "")) {
                    message += "* " + validationName + " bilgisi doğru girilmelidir." + newLine;
                    Library.Array.Add(invalidElements, element.id);
                }
                else if (!Library.Validation.IsTcKimlikNo(element.value)) {
                    message += "* " + validationName + " bilgisi geçerli bir Vatandaşlık Numarası olmalıdır." + newLine;
                    Library.Array.Add(invalidElements, element.id);
                }
            }
            // IBAN Validation
            if (element.value.length != 0 && controlType == "IBAN") {
                if (!Library.Validation.IsIBAN(element.value)) {
                    message += "* " + validationName + " bilgisi geçerli bir Uluslararası Banka Hesap Numarası olmalıdır." + newLine;
                    Library.Array.Add(invalidElements, element.id);
                }
            }
            if (validate == "True") {
                // BaseText validation
                if (element.value.length == 0) {
                    message += "* " + validationName + " bilgisi girilmelidir." + newLine;
                    Library.Array.Add(invalidElements, element.id);
                }
                // NumericBox validation
                if (element.value.length != 0 && controlType == "Numeric") {
                    if (!Library.Validation.IsNumeric(element.value, ",.") || Library.Util.Count(element.value, ",") > 1) {
                        message += "* " + validationName + " bilgisi doğru girilmelidir." + newLine;
                        Library.Array.Add(invalidElements, element.id);
                    }
                    else {
                        var minValue = Library.UserInterface.GetAttribute(element.id, "ValidationMinValue");
                        var maxValue = Library.UserInterface.GetAttribute(element.id, "ValidationMaxValue");

                        if (minValue != "" && eval(minValue) > eval(Library.Util.ReplaceAll(element.value, ".", ""))) {
                            message += "* " + validationName + " alanına en az " + minValue + " değeri girilmelidir." + newLine;
                            Library.Array.Add(invalidElements, element.id);
                        }
                        if (maxValue != "" && eval(maxValue) < eval(Library.Util.ReplaceAll(element.value, ".", ""))) {
                            message += "* " + validationName + " alanına en fazla " + maxValue + " değeri girilmelidir." + newLine;
                            Library.Array.Add(invalidElements, element.id);
                        }
                    }
                }
            }
        }
    }

    if (message != "") {
        Library.Validation.HighlightInvalidElements(invalidElements);
        //alert(message);

        $("#ModalDialog")[0].innerHTML = Library.Util.ReplaceAll(Library.Util.ReplaceAll(message, newLine, "<br />"), "*", "&bull;");
        $($("#ModalDialog")[0]).attr("title", "İşleminizi Kontrol Ediniz");
        $("#ModalDialog").dialog({
            modal: true,
            resizable: false,
            buttons: {
                Tamam: function () {
                    $("#ModalDialog")[0].innerHTML = "";
                    $($("#ModalDialog")[0]).attr("title", "Hata");
                    $(this).dialog("close");
                }
            }
        });

        return false;
    } else {
        return true;
    }
}

Library.Validation.HighlightInvalidElements = function (elementList) {
    var preStyles = new Array();
    for (var i = 0; i < elementList.length; i++) {
        $("#" + elementList[i]).addClass("Invalid");
    }
}